home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Drivers / vc_2_2.lha / ParNet / Source / task.c < prev    next >
C/C++ Source or Header  |  1994-11-01  |  3KB  |  141 lines

  1. /*
  2. ** $Header: SRC:CVSROOT/Vector/ParNet/task.c,v 1.1.1.1 1994/06/23 02:39:41 Barnard Exp $
  3. */
  4.  
  5. /*
  6. ** This code was originally written by Matthew Dillon and put into Public Domain
  7. **
  8. ** All changes concerning the adaption of Matt's original code to the
  9. ** Vector Connection I/O board are © 1991-1994 by Henning Schmiedehausen
  10. ** All rights for this changes are reserved. The original code is Public Domain
  11. **
  12. ** This code is distributed with the expressed written permission of Matthew
  13. ** Dillon (Thank you very much, Matt)
  14. **
  15. */
  16.  
  17. /*
  18.  *  TASK.C
  19.  *
  20.  *  -Accept packets to send to network
  21.  *  -Receive data from network
  22.  *
  23.  *        ParWrite(destaddr, buf, bytes)
  24.  */
  25.  
  26. #include "defs.h"
  27. #include "parnet_asm.h"
  28.  
  29. #include "task_protos.h"
  30. #include "parnet_protos.h"
  31.  
  32. #include <proto/exec.h>
  33.  
  34. typedef struct {
  35.     uword   Port;   /*    destination port    */
  36.     uword   ChkSum; /*    data checksum        */
  37.     ulong   Bytes;  /*    # of bytes        */
  38. } Header;
  39.  
  40. char    DataBuf[MAXPKTSIZE];
  41. Header    Hdr;
  42. long    TLock[2] = { 0, 0 };
  43. static    short Cnt = 0;
  44.  
  45. void
  46. CParNetTask()
  47. {
  48.     long mask;
  49.     Unit *unit;
  50.     Packet *packet;
  51.     long n;
  52.  
  53.     for (;;) {
  54.         SetPIOInt();
  55.     mask = Wait(SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F);
  56.     LockAddr(TLock);
  57.     if (mask & SIGBREAKF_CTRL_F) {      /*  packet pending  */
  58. data:
  59.         if (ParDataReady() > 0) {
  60.         n = ParReadV(&Hdr, sizeof(Hdr), DataBuf, MAXPKTSIZE, NULL, NULL);
  61.  
  62.         /*sprintf(StickyPort->DebugBuf, "%03d READ  %6ld", Cnt++, n);*/
  63.  
  64.         if (n >= sizeof(Hdr) && n >= sizeof(Hdr) + Hdr.Bytes) {
  65.             if (unit = FindUnitForPort(Hdr.Port)) {
  66.             packet = AllocParPacket(NULL, unit, DataBuf, Hdr.Bytes, NULL, 0);
  67.             LockAddr(unit->UnitLock);
  68.             (*packet->io_Unit->DataFunc)('r', packet, n - sizeof(Hdr));
  69.             UnlockAddr(unit->UnitLock);
  70.             }
  71.         }
  72.         }
  73.     }
  74.     if (mask & SIGBREAKF_CTRL_E) {      /*  port            */
  75.         while (packet = (Packet *)GetMsg(&DevBase->Port)) {
  76.         n = OutputPacket(packet);
  77.         if (n == -2) {
  78.             Forbid();
  79.             AddHead(&DevBase->Port.mp_MsgList, (struct Node *)packet);
  80.             Permit();
  81.             goto data;
  82.         }
  83.         }
  84.     }
  85.     UnlockAddr(TLock);
  86.     }
  87. }
  88.  
  89. OutputPacket(packet)
  90. Packet *packet;
  91. {
  92.     long n;
  93.     Unit *unit = packet->io_Unit;
  94.  
  95.     Hdr.Port  = packet->DestPort;
  96.     Hdr.ChkSum= 0;
  97.     Hdr.Bytes = packet->DLen1 + packet->DLen2;
  98.  
  99.     n = ParWriteV(packet->DestAddr, &Hdr, sizeof(Hdr), packet->Data1, packet->DLen1, packet->Data2, packet->DLen2, NULL, NULL);
  100.  
  101.     /* sprintf(StickyPort->DebugBuf, "%03d WRITE %6ld", Cnt++, n); */
  102.  
  103.     if (n == -2)                /*  can't write, pending rcv    */
  104.     return(n);
  105.     LockAddr(unit->UnitLock);
  106.     if (n == sizeof(Hdr) + packet->DLen1 + packet->DLen2)
  107.     (*unit->DataFunc)('w', packet, n - sizeof(Hdr));
  108.     else
  109.     (*unit->DataFunc)('W', packet, n - sizeof(Hdr));
  110.     UnlockAddr(unit->UnitLock);
  111.     return(n);
  112. }
  113.  
  114.  
  115. /*
  116.  *  Queue packet for write.  If QUICKIO is requested and the packet can be
  117.  *  sent manually it is, else it is queued to the task.
  118.  */
  119.  
  120. void
  121. QueuePacketForWrite(packet)
  122. Packet *packet;
  123. {
  124.     Iob *iob = packet->iob;
  125.  
  126.     if ((iob->io_Flags & IOF_QUICK) && TryLockAddr(TLock) > 0) {
  127.     if (OutputPacket(packet) != -2) {
  128.         UnlockAddr(TLock);
  129.         return;
  130.     }
  131.     /*
  132.      *  Can't write packet, rcv packet pending.
  133.      */
  134.     UnlockAddr(TLock);
  135.     }
  136.     iob->io_Flags &= ~IOF_QUICK;
  137.     iob->io_Flags |= IOF_QUEUED;
  138.     PutMsg(&DevBase->Port, &packet->Msg);
  139. }
  140.  
  141.